﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KP_CommonLibrary.Logs;
using KP_CommonLibrary.KP_CommonControls;
using KP_CommonLibrary;
using KP_CommonLibrary.KP_Interface;
using KEEPER_BRR_13dotNET.BusinessLogic;
using KP_CommonLibrary.KP_Function;

namespace KEEPER_BRR_13dotNET.Presentation
{
    public partial class Ucl_DsHoSoThamDinh : KP_CommonLibrary.KP_CommonControls.KP_UserControl, IRunCommand
    {
        BL_KPHoSo Bussines = null;
        int ma_tt1, ma_tt2;
        int ma_duyet, ma_tl;
        int isFinish = 0;
        string maVtri = "";
        string tenVtri = "";
        string maVtriNhan = "";
        string tenVitriNhan = "";
        public Ucl_DsHoSoThamDinh()
        {
            InitializeComponent();
            if (!DesignMode)
            {
                Bussines = new BL_KPHoSo();
               // Bussines.InitDependencyBussines();
            }
        }

        private void MainGrid_RowDoubleClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs e)
        {
            DataRow row = null;
            FrmE_HoSoMoi frm = null;
            try
            {
                row = MainGrid.GetCurrentRow();
                frm = new FrmE_HoSoMoi();
                //frm.ViewDetail(row, true, biChapNhan_Click, biTraLai_Click);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Xem thông tin hồ sơ", ex);
                KPMessageBox.Show("Xem thông tin hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }
        }

        #region IRunCommand Members

        public void RunCommand(object sender, object commandData)
        {
            string _params = (commandData as DataRow)["Params"].ToString();
            Dictionary<string, string> allParams = ArgsParser.Parse1(_params);
            string value = "";
            if (allParams.TryGetValue("TrangThai1", out value))
                ma_tt1 = int.Parse(value);
            else
                ma_tt1 = -1;

            if (allParams.TryGetValue("TrangThai2", out value))
                ma_tt2 = int.Parse(value);
            else
                ma_tt2 = -1;

            if (allParams.TryGetValue("Duyet", out value))
                ma_duyet = int.Parse(value);
            else
                ma_duyet = -1;

            if (allParams.TryGetValue("TraLai", out value))
                ma_tl = int.Parse(value);
            else
                ma_tl = -1;

            if (allParams.TryGetValue("KetThuc", out value))
                isFinish = int.Parse(value);
            else
                isFinish = 0;

            if (allParams.TryGetValue("ViTri", out value))
                maVtri = value;
            else
                maVtri = CauHinh.MaViTri1();
            if (allParams.TryGetValue("ViTriNhan", out value))
                maVtriNhan = value;
            else
                maVtriNhan = CauHinh.MaViTri1();
            BL_KPDmVTri blViTri = new BL_KPDmVTri();

            tenVitriNhan = blViTri.GetTenViTri(maVtriNhan);
            tenVtri = blViTri.GetTenViTri(maVtri);


            AppContext.MainForm.ShowControlInTab(this, sender != null ? sender.ToString() : "Danh sách hồ sơ");

        }



        #endregion

        private void biChapNhan_Click(object sender, EventArgs e)
        {
            try
            {
                if (MainGrid.PrimaryGrid.ActiveRow != null)
                {
                    DataRow row = MainGrid.GetCurrentRow();
                    Frm_NhapLyDoDuyet frmLyDo = new Frm_NhapLyDoDuyet();
                    frmLyDo.ShowDialog();
                    if (frmLyDo.DialogResult == DialogResult.OK)
                    {
                        if (isFinish == 0)
                            Bussines.DuyetHoSo(row["Stt_Rec"].ToString(), Convert.ToInt32(row["Ma_Tt"]), ma_duyet, frmLyDo.LyDo, (int)KieuDuyet.DongY);
                        else
                        {
                            Bussines.KetThucDuyetHs(row["Stt_Rec"].ToString(), Convert.ToInt32(row["Ma_Tt"]), ma_duyet, frmLyDo.LyDo);
                        }
                        KPMessageBox.Show("Duyệt hồ sơ " + row["So_Gp"].ToString() + " thành công", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Information);

                    }
                    frmLyDo.Dispose();
                }
                else
                    KPMessageBox.Show("Chưa chọn hồ sơ cần duyệt", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Duyệt hồ sơ", ex);
                KPMessageBox.Show("Duyệt hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }

        }

        private void biTraLai_Click(object sender, EventArgs e)
        {
            try
            {
                if (MainGrid.PrimaryGrid.ActiveRow != null)
                {
                    DataRow row = MainGrid.GetCurrentRow();
                    Frm_NhapLyDoDuyet frmLyDo = new Frm_NhapLyDoDuyet();
                    frmLyDo.ShowDialog();
                    if (frmLyDo.DialogResult == DialogResult.OK)
                    {
                        Bussines.DuyetHoSo(row["Stt_Rec"].ToString(), Convert.ToInt32(row["Ma_Tt"]), ma_tl, frmLyDo.LyDo, (int)KieuDuyet.TuChoi);
                    }
                    frmLyDo.Dispose();
                    KPMessageBox.Show("Duyệt hồ sơ " + row["So_Gp"].ToString() + " thành công", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Information);
                }
                else
                    KPMessageBox.Show("Chưa chọn hồ sơ cần duyệt", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Duyệt hồ sơ", ex);
                KPMessageBox.Show("Duyệt hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }

        private void Ucl_DsHoSoThamDinh_Load(object sender, EventArgs e)
        {
            try
            {
                MainGrid.PrimaryGrid.DataSource = Bussines.FindDangKyKdByKeyword(txtGiaTriCanTim.Text, ma_tt1, ma_tt2, maVtri);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Lấy danh sách hồ sơ cần thẩm định", ex);
                KPMessageBox.Show("Lấy danh sách hồ sơ cần thẩm định:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }

        private void txtGiaTriCanTim_ButtonCustomClick(object sender, EventArgs e)
        {
            Ucl_DsHoSoThamDinh_Load(this, e);
        }

        private void MainGrid_PreRenderRow(object sender, DevComponents.DotNetBar.SuperGrid.GridPreRenderRowEventArgs e)
        {
            DataRow row = MainGrid.GetDataRow(e.GridRow.RowIndex);
            if (row != null)
            {
                if (row["Ma_Tt"].ToString() == ma_tt1.ToString())
                {
                    e.GridRow.CellStyles.Default.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color2 = Color.GreenYellow;

                }
                if (row["Ma_Tt"].ToString() == ma_tt2.ToString())
                {
                    e.GridRow.CellStyles.Default.Background.Color1 = Properties.Settings.Default.CancelColor; ;
                    e.GridRow.CellStyles.Selected.Background.Color1 = Properties.Settings.Default.CancelColor;
                }



            }
        }

        private void biChuyenHs_Click(object sender, EventArgs e)
        {
            Form frm = new Form();
            frm.Text = "Chuyển hồ sơ tới " + tenVitriNhan;
            try
            {
                Ucl_ChuyenHoSo uc = new Ucl_ChuyenHoSo();
                uc.MaViTri = maVtri;
                uc.MaViTriNhan = maVtriNhan;
                uc.TenViTri = tenVtri;
                uc.TenViTriNhan = tenVitriNhan;
                uc.MaTT = ma_duyet.ToString();

                frm.Size = uc.Size;
                uc.Dock = DockStyle.Fill;
                frm.Controls.Add(uc);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Chuyển hồ sơ", ex);
                KPMessageBox.Show("Gặp lỗi khi chuyển hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                frm.Dispose();
            }
        }
    }
    public enum KieuDuyet
    {
        DongY,
        TuChoi
    }
}
